package keeto.com.vn.bms.ui.activity;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.model.BMSUserModel;
import keeto.com.vn.bms.model.CompanyModel;
import keeto.com.vn.bms.model.Hospital;
import keeto.com.vn.bms.model.InfoReportModel;
import keeto.com.vn.bms.model.MedicalRecordModel;
import keeto.com.vn.bms.model.report.DetailReportDoctorModel;
import keeto.com.vn.bms.model.report.DetailReportHospitalModel;
import keeto.com.vn.bms.model.report.ReportCompanyModel;
import keeto.com.vn.bms.model.report.ReportDoctorModel;
import keeto.com.vn.bms.model.report.ReportHospitalModel;
import keeto.com.vn.bms.model.report.ReportMedicalRecordModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.adapter.ListFileSDCardAdapter;
import keeto.com.vn.bms.ui.adapter.ListMultiChoiceCompanyAdapter;
import keeto.com.vn.bms.ui.adapter.ListMultiChoiceDoctorAdapter;
import keeto.com.vn.bms.ui.adapter.ListMultiChoiceHospitalAdapter;
import keeto.com.vn.bms.ui.adapter.ListMultiChoiceMedicalRecordAdapter;
import keeto.com.vn.bms.ui.adapter.ReportDetailDoctorAdapter;
import keeto.com.vn.bms.ui.adapter.ReportDetailHospitalAdapter;
import keeto.com.vn.bms.ui.widget.DeleteDialog;
import keeto.com.vn.bms.ui.widget.IPopupWindow;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ReportDetailActivity extends Activity implements OnClickListener,
OnItemClickListener,OnItemLongClickListener {

	private static final String KEY_REPORT = "Key_ Report";
	private ImageButton btnBack;
	private TextView tvTitle;
	private InfoReportModel reportModel;
	private RelativeLayout rlReporthospital;
	private Button btnDetail_hospital;
	private RelativeLayout rlReportdoctor;
	private Button btnDetail_doctor;
	private RelativeLayout rlReportcompany;
	private Button btnDetail_company;
	private RelativeLayout rlReportmedical;
	private Button btnDetail_medical;
	private Button edtFrom_time;
	private Button edtTo_time;
	private Button btnSendReport;
	private LinearLayout llDetailReport;
	private TextView tvNamereport;
	private TextView tvTimereport;
	private ListView mListView;
	private ListView lvSub;
	private TextView tvSubName;
	private Button btnExportRecord;
	private ImageView imgLoading;
	private Button btnSumSurgery;
	private Button btnSumInCome;
	private ProgressBar pBarReport;

	/*
	 * Hospital
	 */
	private ArrayList<ReportHospitalModel> mArrHospital = new ArrayList<ReportHospitalModel>();
	private ListMultiChoiceHospitalAdapter choiceHospitalAdapter;
	private ArrayList<DetailReportHospitalModel> mArrHos = new ArrayList<DetailReportHospitalModel>();
	private ReportDetailHospitalAdapter hospitalAdapter;
	private ArrayList<Hospital> listHopitals = new ArrayList<Hospital>();
	private IPopupWindow popupHospital;
	TextView tvHospital;

	/*
	 * Doctor
	 */
	private IPopupWindow popupDoctor;
	private ArrayList<ReportDoctorModel> mArrDoctor = new ArrayList<ReportDoctorModel>();
	private ListMultiChoiceDoctorAdapter choiceDoctorAdapter;
	private ArrayList<DetailReportDoctorModel> mArrReportDoctor = new ArrayList<DetailReportDoctorModel>();
	private ReportDetailDoctorAdapter detailDoctorAdapter;
	private ArrayList<BMSUserModel> listDoctors = new ArrayList<BMSUserModel>();
	private BMSSharedPreferences mPreferences;
	private int GroupIDDoctor = 3;
	TextView tvDoctor;
	private static int pageNumberDoctor = 1;;
	private final static int PAGE_LIMIT_DOCTOR = 10;

	/*
	 * Company
	 */
	private IPopupWindow popupCompany;
	private ArrayList<ReportCompanyModel> mArrCompany = new ArrayList<ReportCompanyModel>();
	private ListMultiChoiceCompanyAdapter choiceCompanyAdapter;
	private ArrayList<CompanyModel> listCompanies = new ArrayList<CompanyModel>();
	TextView tvCompany;

	/*
	 * Medical Record
	 */
	private IPopupWindow popupMedicalRecord;
	private ArrayList<ReportMedicalRecordModel> mArrMedicalRecord = new ArrayList<ReportMedicalRecordModel>();
	private ListMultiChoiceMedicalRecordAdapter choiceMedicalRecordAdapter;
	private ArrayList<MedicalRecordModel> listMedicalRescords = new ArrayList<MedicalRecordModel>();
	TextView tvRecord;
	private ReportDetailListener mListener = new ReportDetailListener();

	TextView tvTime,tvFrom,tvTo;
	/*
	 * DateTime Picker
	 */
	private Calendar cal;
	int mYear;
	int mMonth;
	int mDay;
	private String DateFrom = "";
	private String DateTo = "";
	/*
	 * File
	 */
	private ArrayList<File> listFiles = new ArrayList<File>();
	private ListFileSDCardAdapter fileSDCardAdapter;
	DeleteDialog dialog;

	private int SortBySugery = 3;
	private int SortByIncome = 1;
	private int typeReport;
	private StringBuilder ObjID;
	private int typeCompare;
	private String PathFile = null;
	private ArrayList<File> listRevertedFiles= new ArrayList<File>();
	private int checkSpinner=0;
	private static int positionToDelete;
	private static String sortDoctor; 

	public static void actionLaunch(Context context, InfoReportModel model) {
		Intent it = new Intent(context, ReportDetailActivity.class);
		it.putExtra(KEY_REPORT, model);
		context.startActivity(it);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report_detail_layout);
		Intent it = getIntent();
		Bundle extra = it.getExtras();
		reportModel = (InfoReportModel) extra.get(KEY_REPORT);
		mPreferences = new BMSSharedPreferences(this);
		initUIComponent();

		setViewbyType(reportModel.getTypeReport());

	}

	private void initUIComponent() {
		btnBack = (ImageButton) findViewById(R.id.btn_tittle_back);
		tvTitle = (TextView) findViewById(R.id.tv_title_name);

		tvHospital=(TextView)findViewById(R.id.tv_detail_hospital);
		tvCompany=(TextView)findViewById(R.id.tv_detail_company);
		tvDoctor=(TextView)findViewById(R.id.tv_detail_doctor);
		tvRecord=(TextView)findViewById(R.id.tv_detail_medical_record);
		tvFrom=(TextView)findViewById(R.id.tv_detail_from_time);
		tvTime=(TextView)findViewById(R.id.tv_detail_time);
		tvTo=(TextView)findViewById(R.id.tv_detail_to_time);

		tvHospital.setText(Html.fromHtml(getString(R.string.detail_hospital) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		tvCompany.setText(Html.fromHtml(getString(R.string.detail_company) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		tvDoctor.setText(Html.fromHtml(getString(R.string.detail_doctor) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		tvRecord.setText(Html.fromHtml(getString(R.string.detail_medical_record) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		tvFrom.setText(Html.fromHtml(getString(R.string.detail_from_time) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		tvTime.setText(Html.fromHtml(getString(R.string.detail_time) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		tvTo.setText(Html.fromHtml(getString(R.string.detail_to_time) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));

		rlReporthospital = (RelativeLayout) findViewById(R.id.rl_report_hospital);
		btnDetail_hospital = (Button) findViewById(R.id.btn_sp_detail_hospital);
		rlReportdoctor = (RelativeLayout) findViewById(R.id.rl_report_doctor);
		btnDetail_doctor = (Button) findViewById(R.id.btn_sp_detail_doctor);
		rlReportcompany = (RelativeLayout) findViewById(R.id.rl_report_company);
		btnDetail_company = (Button) findViewById(R.id.btn_sp_detail_company);
		rlReportmedical = (RelativeLayout) findViewById(R.id.rl_repord_medical);
		btnDetail_medical = (Button) findViewById(R.id.btn_sp_detail_medical_record);

		edtFrom_time = (Button) findViewById(R.id.edt_detail_from_time);
		edtTo_time = (Button) findViewById(R.id.edt_detail_to_time);

		btnSendReport = (Button) findViewById(R.id.btn_detail_send_report);

		llDetailReport = (LinearLayout) findViewById(R.id.ll_detail_report_by);
		tvNamereport = (TextView) findViewById(R.id.tv_detail_report_by);
		tvTimereport = (TextView) findViewById(R.id.tv_detail_report_time);
		lvSub = (ListView) findViewById(R.id.listViewSubDetailReport);
		tvSubName = (TextView) findViewById(R.id.tv_detail_report_name_sortby);
		btnExportRecord = (Button) findViewById(R.id.btn_export_report);
		btnExportRecord.setOnClickListener(this);
		imgLoading = (ImageView) findViewById(R.id.img_loading_statistic);
		imgLoading.setVisibility(View.GONE);
		// imgLoading
		// .setBackgroundResource(R.drawable.animation_statistic);

		mListView = (ListView) findViewById(R.id.lviewDetailReport);
		btnSumSurgery = (Button) findViewById(R.id.btnSumSergery);
		btnSumInCome = (Button) findViewById(R.id.btnSumInCome);
		pBarReport = (ProgressBar) findViewById(R.id.pBarReport);
		pBarReport.setVisibility(View.GONE);

		btnBack.setOnClickListener(this);
		tvTitle.setText(reportModel.getReportName());
		btnDetail_hospital.setText(this.getResources().getString(
				R.string.detail_hospital_des));
		btnDetail_doctor.setText(this.getResources().getString(
				R.string.detail_doctor_des));
		btnDetail_company.setText(this.getResources().getString(
				R.string.detail_company_des));
		btnDetail_medical.setText(this.getResources().getString(
				R.string.detail_medical_record_des));
		tvNamereport.setText(this.getResources().getString(
				R.string.detail_record_name)
				+ reportModel.getReportName());
		btnDetail_hospital.setOnClickListener(this);
		btnDetail_doctor.setOnClickListener(this);
		btnDetail_company.setOnClickListener(this);
		btnDetail_medical.setOnClickListener(this);
		edtFrom_time.setOnClickListener(this);
		edtTo_time.setOnClickListener(this);
		btnSendReport.setOnClickListener(this);
		btnSumInCome.setOnClickListener(this);
		btnSumSurgery.setOnClickListener(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnItemLongClickListener(this);

		rlReporthospital.setVisibility(View.GONE);
		rlReportdoctor.setVisibility(View.GONE);
		rlReportcompany.setVisibility(View.GONE);
		rlReportmedical.setVisibility(View.GONE);
		llDetailReport.setVisibility(View.GONE);
		mListView.setVisibility(View.GONE);
		cal = Calendar.getInstance();
		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH);
		mDay = cal.get(Calendar.DAY_OF_MONTH);
	}

	private void setViewbyType(String typereport) {
		if (typereport.equals(IBMSConfig.KEY_REPORT_BY_HOSPITAL)) {
			rlReporthospital.setVisibility(View.VISIBLE);
			rlReportdoctor.setVisibility(View.GONE);
			rlReportcompany.setVisibility(View.GONE);
			rlReportmedical.setVisibility(View.GONE);
			llDetailReport.setVisibility(View.GONE);
			mListView.setVisibility(View.GONE);
			BMSCore.getInstance(this).taskGetHopital(
					mPreferences.getUserAuthKey(this), mListener);
			pBarReport.setVisibility(View.VISIBLE);
		} else if (typereport.equals(IBMSConfig.KEY_REPORT_BY_DOCTOR)) {
			rlReporthospital.setVisibility(View.GONE);
			rlReportdoctor.setVisibility(View.VISIBLE);
			rlReportcompany.setVisibility(View.GONE);
			rlReportmedical.setVisibility(View.GONE);
			llDetailReport.setVisibility(View.GONE);
			mListView.setVisibility(View.GONE);
			BMSCore.getInstance(this).taskGetTypeUser(mPreferences.getUserAuthKey(this),GroupIDDoctor,PAGE_LIMIT_DOCTOR,pageNumberDoctor, mListener);
			pBarReport.setVisibility(View.VISIBLE);

		} else if (typereport
				.equals(IBMSConfig.KEY_REPORT_COMPARE_SUM_BY_HOSPITAL)) {
			rlReporthospital.setVisibility(View.GONE);
			rlReportdoctor.setVisibility(View.GONE);
			rlReportcompany.setVisibility(View.VISIBLE);
			rlReportmedical.setVisibility(View.VISIBLE);
			llDetailReport.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			PathFile = Utils
					.getTempFileDir(IBMSConfig.PATH_SD_COMPARE_OPERATION);
			if (PathFile != null) {
				loadListFileFromSDCard(PathFile);
			}
			BMSCore.getInstance(this).taskGetListCompany(
					mPreferences.getUserAuthKey(this), mListener);
			BMSCore.getInstance(this).taskGetListMedicalRecord(
					mPreferences.getUserAuthKey(this), mListener);
			pBarReport.setVisibility(View.VISIBLE);

		} else if (typereport
				.equals(IBMSConfig.KEY_REPORT_COMPARE_IMCOME_BY_HOSPITAL)) {
			rlReporthospital.setVisibility(View.GONE);
			rlReportdoctor.setVisibility(View.GONE);
			rlReportcompany.setVisibility(View.VISIBLE);
			rlReportmedical.setVisibility(View.GONE);
			llDetailReport.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			PathFile = Utils.getTempFileDir(IBMSConfig.PATH_SD_COMPARE_INCOME);
			if (PathFile != null) {
				loadListFileFromSDCard(PathFile);
			}
			BMSCore.getInstance(this).taskGetListCompany(
					mPreferences.getUserAuthKey(this), mListener);
			pBarReport.setVisibility(View.VISIBLE);

		} else if (typereport.equals(IBMSConfig.KEY_REPORT_COMPARE_BY_ALL)) {
			rlReporthospital.setVisibility(View.GONE);
			rlReportdoctor.setVisibility(View.GONE);
			rlReportcompany.setVisibility(View.VISIBLE);
			rlReportmedical.setVisibility(View.GONE);
			llDetailReport.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			PathFile = Utils.getTempFileDir(IBMSConfig.PATH_SD_COMPARE_ALL);
			if (PathFile != null) {
				loadListFileFromSDCard(PathFile);
			}
			BMSCore.getInstance(this).taskGetListCompany(
					mPreferences.getUserAuthKey(this), mListener);
			pBarReport.setVisibility(View.VISIBLE);

		}
	}

	private void PopupHospital() {
		if (popupHospital == null) {
			mArrHospital.clear();
			for (int i = 0; i < listHopitals.size(); i++) {
				mArrHospital.add(new ReportHospitalModel(listHopitals.get(i)
						.getId() + "", listHopitals.get(i).getName(), false));
			}

			popupHospital = new IPopupWindow(this,
					R.layout.popup_sortby_layout, null, false,
					btnDetail_hospital.getMeasuredWidth(),
					LayoutParams.WRAP_CONTENT);
			ListView listPopup = (ListView) popupHospital.getView()
					.findViewById(R.id.listPopup);
			choiceHospitalAdapter = new ListMultiChoiceHospitalAdapter(this,
					mArrHospital, btnDetail_hospital);
			listPopup.setAdapter(choiceHospitalAdapter);

			popupHospital.setAutoHideOutside();
			popupHospital.showAsDropDown(btnDetail_hospital);
		} else {
			popupHospital.showAsDropDown(btnDetail_hospital);
		}
	}

	private void PopupDoctor() {
		if (popupDoctor == null) {
			mArrDoctor.clear();
			for (int i = 0; i < listDoctors.size(); i++) {
				mArrDoctor.add(new ReportDoctorModel(listDoctors.get(i)
						.getUserID() + "", listDoctors.get(i).getFullName(),
						false));
			}
			popupDoctor = new IPopupWindow(this, R.layout.popup_sortby_layout,
					null, false, btnDetail_doctor.getMeasuredWidth(),
					LayoutParams.WRAP_CONTENT);
			ListView listPopup = (ListView) popupDoctor.getView().findViewById(
					R.id.listPopup);
			choiceDoctorAdapter = new ListMultiChoiceDoctorAdapter(
					ReportDetailActivity.this, mArrDoctor, btnDetail_doctor);
			listPopup.setAdapter(choiceDoctorAdapter);

			popupDoctor.setAutoHideOutside();
			popupDoctor.showAsDropDown(btnDetail_doctor);
		} else {
			popupDoctor.showAsDropDown(btnDetail_doctor);
		}
	}

	private void PopupCompany() {
		if (popupCompany == null) {
			mArrCompany.clear();
			for (int i = 0; i < listCompanies.size(); i++) {
				mArrCompany.add(new ReportCompanyModel(listCompanies.get(i)
						.getComID() + "", listCompanies.get(i).getComName(),
						false));
			}
			popupCompany = new IPopupWindow(this, R.layout.popup_sortby_layout,
					null, false, btnDetail_company.getMeasuredWidth(),
					LayoutParams.WRAP_CONTENT);
			ListView listPopup = (ListView) popupCompany.getView()
					.findViewById(R.id.listPopup);
			choiceCompanyAdapter = new ListMultiChoiceCompanyAdapter(this,
					mArrCompany, btnDetail_company);
			listPopup.setAdapter(choiceCompanyAdapter);

			popupCompany.setAutoHideOutside();
			popupCompany.showAsDropDown(btnDetail_company);
		} else {
			popupCompany.showAsDropDown(btnDetail_company);
		}
	}

	private void PopupMedicalRecord() {
		if (popupMedicalRecord == null) {

			for (int i = 0; i < listMedicalRescords.size(); i++) {
				mArrMedicalRecord.add(new ReportMedicalRecordModel(
						listMedicalRescords.get(i).getId() + "",
						listMedicalRescords.get(i).getNameRecord(), false));
			}

			popupMedicalRecord = new IPopupWindow(this,
					R.layout.popup_sortby_layout, null, false,
					btnDetail_medical.getMeasuredWidth(),
					LayoutParams.WRAP_CONTENT);
			ListView listPopup = (ListView) popupMedicalRecord.getView()
					.findViewById(R.id.listPopup);
			choiceMedicalRecordAdapter = new ListMultiChoiceMedicalRecordAdapter(
					this, mArrMedicalRecord, btnDetail_medical);
			listPopup.setAdapter(choiceMedicalRecordAdapter);
			popupMedicalRecord.setAutoHideOutside();
			popupMedicalRecord.showAsDropDown(btnDetail_medical);
		} else {
			popupMedicalRecord.showAsDropDown(btnDetail_medical);
		}
	}

	private void reportbyHospital() {
		if (choiceHospitalAdapter != null) {
			typeReport = 1;
			ObjID = new StringBuilder();
			ObjID.append("[");
			for (int i = 0; i < mArrHospital.size(); i++) {
				if (mArrHospital.get(i).isSelected()) {
					ObjID.append("\"");
					ObjID.append(mArrHospital.get(i).getIdhospital());
					ObjID.append("\",");
				}
			}
			String mObjID = ObjID.toString().substring(0, ObjID.length() - 1)
					+ "]";

			BMSCore.getInstance(ReportDetailActivity.this)
			.taskGetDetailReportByType(
					typeReport,
					mObjID,
					SortByIncome,
					DateFrom,
					DateTo,
					mPreferences
					.getUserAuthKey(ReportDetailActivity.this),
					mListener);
		}
	}

	private void reportbyDoctor() {
		if (choiceDoctorAdapter != null) {
			typeReport = 2;
			ObjID = new StringBuilder();
			ObjID.append("[");
			for (int i = 0; i < mArrDoctor.size(); i++) {
				if (mArrDoctor.get(i).isSelected()) {
					ObjID.append("\"");
					ObjID.append(mArrDoctor.get(i).getDoctorId());
					ObjID.append("\",");
				}
			}
			String mObjID = ObjID.toString().substring(0, ObjID.length() - 1)
					+ "]";
			sortDoctor=mObjID;

			BMSCore.getInstance(ReportDetailActivity.this)
			.taskGetDetailReportByType(
					typeReport,
					mObjID,
					SortByIncome,
					DateFrom,
					DateTo,
					mPreferences
					.getUserAuthKey(ReportDetailActivity.this),
					mListener);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			this.finish();
			break;

		case R.id.btn_sp_detail_hospital:
			PopupHospital();
			break;
		case R.id.btn_sp_detail_doctor:
			PopupDoctor();
			break;
		case R.id.btn_sp_detail_company:
			PopupCompany();
			break;
		case R.id.btn_sp_detail_medical_record:

			PopupMedicalRecord();
			break;
		case R.id.btn_detail_send_report:
			validateReport();
			break;
		case R.id.btn_export_report:
			if (reportModel.getTypeReport().equals(
					IBMSConfig.KEY_REPORT_BY_HOSPITAL)
					&& mArrHos.size() > 0) {

				saveExcelFile(this, mArrHos, null,
						IBMSConfig.KEY_REPORT_BY_HOSPITAL);
			} else if (reportModel.getTypeReport().equals(
					IBMSConfig.KEY_REPORT_BY_DOCTOR)
					&& mArrReportDoctor.size() > 0) {
				saveExcelFile(this, null, mArrReportDoctor,
						IBMSConfig.KEY_REPORT_BY_DOCTOR);

			}

			break;
		case R.id.edt_detail_from_time:
			showDatePickerDialogFrom();
			break;
		case R.id.edt_detail_to_time:
			showDatePickerDialogTo();
			break;
		case R.id.btnSumSergery:
			btnSumInCome.setCompoundDrawables(null, null, null, null);
			if (SortBySugery == 3) {
				btnSumSurgery.setCompoundDrawables(
						null,
						null,
						this.getResources().getDrawable(
								R.drawable.icon_triangle_down), null);
				BMSCore.getInstance(ReportDetailActivity.this)
				.taskGetDetailReportByType(
						typeReport,
						sortDoctor,
						SortByIncome,
						DateFrom,
						DateTo,
						mPreferences
						.getUserAuthKey(ReportDetailActivity.this),
						mListener);
				SortBySugery = 4;
			} else if(SortBySugery == 4) {
				btnSumSurgery.setCompoundDrawables(null, null, this
						.getResources()
						.getDrawable(R.drawable.icon_triangle_up), null);
				BMSCore.getInstance(ReportDetailActivity.this)
				.taskGetDetailReportByType(
						typeReport,
						sortDoctor,
						SortByIncome,
						DateFrom,
						DateTo,
						mPreferences
						.getUserAuthKey(ReportDetailActivity.this),
						mListener);
				SortBySugery = 3;
			}

			break;
		case R.id.btnSumInCome:
			btnSumSurgery.setCompoundDrawables(null, null, null, null);
			if (SortBySugery == 1) {
				btnSumSurgery.setCompoundDrawables(
						null,
						null,
						this.getResources().getDrawable(
								R.drawable.icon_triangle_down), null);
				SortByIncome = 2;
			} else {
				btnSumSurgery.setCompoundDrawables(null, null, this
						.getResources()
						.getDrawable(R.drawable.icon_triangle_up), null);
				SortByIncome = 1;
			}
			break;
		default:
			break;
		}
	}

	private void validateReport() {
		if (!DateFrom.equals("") && !DateTo.equals("")) {

			if (reportModel.getTypeReport().equals(
					IBMSConfig.KEY_REPORT_BY_HOSPITAL)) {
				//				llDetailReport.setVisibility(View.VISIBLE);
				mListView.setVisibility(View.GONE);
				tvTimereport.setText(this.getResources().getString(
						R.string.detail_from_time)
						+ DateFrom
						+ this.getResources().getString(
								R.string.detail_record_to_time) + DateTo);
				tvSubName.setText(this.getResources().getString(
						R.string.detail_hospital_sub_name));

				reportbyHospital();

			} else if (reportModel.getTypeReport().equals(
					IBMSConfig.KEY_REPORT_BY_DOCTOR)) {
				//				llDetailReport.setVisibility(View.VISIBLE);
				mListView.setVisibility(View.GONE);
				tvTimereport.setText(this.getResources().getString(
						R.string.detail_from_time)
						+ DateFrom
						+ this.getResources().getString(
								R.string.detail_record_to_time) + DateTo);
				tvSubName.setText(this.getResources().getString(
						R.string.detail_doctor_sub_name));
				reportbyDoctor();

			} else if (reportModel.getTypeReport().equals(
					IBMSConfig.KEY_REPORT_COMPARE_SUM_BY_HOSPITAL)) {
				getFileFromServerbyOperation();
			} else if (reportModel.getTypeReport().equals(
					IBMSConfig.KEY_REPORT_COMPARE_IMCOME_BY_HOSPITAL)) {
				getFileFromServerbyIncome();
			} else {
				getFileFromServerbyAll();
			}
		} else {
			Toast.makeText(
					ReportDetailActivity.this,
					ReportDetailActivity.this.getResources().getString(
							R.string.error_time_report), Toast.LENGTH_SHORT)
							.show();
		}
	}

	private void loadListFileFromSDCard(String path) {
		File yourDir = new File(path);
		listFiles.clear();

		for (File f : yourDir.listFiles()) {
			if (f.isFile()) {
				String name = f.getName();
				if (name.endsWith(".pdf")) {
					listFiles.add(f);
					Log.e("FILE", "FILE  " + f.getName());
				}
			}

		}
		listRevertedFiles.clear();
		for(int i= listFiles.size()-1;i>=0;i--){
			listRevertedFiles.add(listFiles.get(i));
		}
		fileSDCardAdapter = new ListFileSDCardAdapter(
				ReportDetailActivity.this, listRevertedFiles);
		fileSDCardAdapter.areAllItemsEnabled();
		mListView.setAdapter(fileSDCardAdapter);
		Utils.setListViewHeightBasedOnChildrenforList(
				ReportDetailActivity.this, mListView);


	}

	private static boolean saveExcelFile(Context context,
			ArrayList<DetailReportHospitalModel> ListHospital,
			ArrayList<DetailReportDoctorModel> ListDoctor, String keyExport) {

		// check if available and not read only
		if (!Utils.isExternalStorageAvailable()
				|| Utils.isExternalStorageReadOnly()) {
			Log.w("FileUtils", "Storage not available or read only");
			return false;
		}

		boolean success = false;

		// New Workbook
		Workbook wb = new HSSFWorkbook();

		// New Sheet
		Sheet mSheet = null;
		mSheet = wb.createSheet("Sheet1");
		HashMap<Integer, Object[]> data = new HashMap<Integer, Object[]>();
		int Keycell = 0;
		if (keyExport.equals(IBMSConfig.KEY_REPORT_BY_HOSPITAL)) {
			data.put(
					Keycell,
					new Object[] {
							"STT",
							context.getResources().getString(
									R.string.detail_hospital_sub_name),
									context.getResources().getString(
											R.string.detail_number_surgery),
											context.getResources().getString(
													R.string.detail_number_income) });
			for (int i = 0; i < ListHospital.size(); i++) {
				Keycell++;
				data.put(Keycell, new Object[] { i,
						ListHospital.get(i).getHospitalName(),
						ListHospital.get(i).getOperationCount(),
						ListHospital.get(i).getRevenue() });
			}
		} else {
			data.put(
					Keycell,
					new Object[] {
							"STT",
							context.getResources().getString(
									R.string.detail_doctor_sub_name),
									context.getResources().getString(
											R.string.detail_number_surgery),
											context.getResources().getString(
													R.string.detail_number_income) });
			for (int i = 0; i < ListDoctor.size(); i++) {
				Keycell++;
				data.put(Keycell, new Object[] { i,
						ListDoctor.get(i).getDoctorName(),
						ListDoctor.get(i).getSumMedicalRecord(),
						ListDoctor.get(i).getInComeDoctor() });
			}
		}

		Map<Integer, Object[]> mymap = new TreeMap<Integer, Object[]>(data);
		Set<Integer> keyset = mymap.keySet();
		int rowNum = 0;
		for (Integer key : keyset) {
			Row row = mSheet.createRow(rowNum++);
			Object[] objArr = data.get(key);
			int cellNum = 0;
			for (Object obj : objArr) {
				Cell cell = row.createCell(cellNum++);
				if (obj instanceof Date)
					cell.setCellValue((Date) obj);
				else if (obj instanceof Boolean)
					cell.setCellValue((Boolean) obj);
				else if (obj instanceof String)
					cell.setCellValue((String) obj);
				else if (obj instanceof Double)
					cell.setCellValue((Double) obj);
				else if (obj instanceof Integer)
					cell.setCellValue((Integer) obj);
			}
		}

		String pathFile = null;
		pathFile = Utils.getTempFileDir(IBMSConfig.PATH_SD_EXPORT);
		if (pathFile != null) {
			String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
			.format(new Date());
			String fileName;
			if (keyExport.equals(IBMSConfig.KEY_REPORT_BY_HOSPITAL)) {
				fileName = "BMSReportbyHospital" + timeStamp + ".xls";

			} else {
				fileName = "BMSReportbyDoctor" + timeStamp + ".xls";
			}
			File file = new File(pathFile, fileName);
			FileOutputStream os = null;
			try {
				os = new FileOutputStream(file);
				wb.write(os);
				Log.w("FileUtils", "Writing file" + file);
				Log.w("pathFile", pathFile);

				success = true;
				Toast.makeText(
						context,
						"Success!. File have been exported in "
								+ file.getAbsolutePath().toString(),
								Toast.LENGTH_SHORT).show();
			} catch (IOException e) {
				Log.w("FileUtils", "Error writing " + file, e);
			} catch (Exception e) {
				Log.w("FileUtils", "Failed to save file", e);
			} finally {
				try {
					if (null != os)
						os.close();
				} catch (Exception ex) {
				}
			}
		}

		return success;
	}

	private void showDatePickerDialogFrom() {

		DatePickerDialog pic = new DatePickerDialog(ReportDetailActivity.this,
				new OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year,
					int monthOfYear, int dayOfMonth) {
				// TODO Auto-generated method stub
				edtFrom_time.setText(dayOfMonth + "/"
						+ (monthOfYear + 1));
				DateFrom = dayOfMonth + "/" + (monthOfYear + 1) + "/"
						+ year;
			}
		}, mYear, mMonth, mDay);
		pic.setTitle(getResources().getString(R.string.select_date_start));
		pic.show();
	}

	private void showDatePickerDialogTo() {

		DatePickerDialog pic = new DatePickerDialog(ReportDetailActivity.this,
				new OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year,
					int monthOfYear, int dayOfMonth) {
				// TODO Auto-generated method stub
				edtTo_time
				.setText(dayOfMonth + "/" + (monthOfYear + 1));
				DateTo = dayOfMonth + "/" + (monthOfYear + 1) + "/"
						+ year;
			}
		}, mYear, mMonth, mDay);
		pic.setTitle(getResources().getString(R.string.select_date_start));
		pic.show();
	}

	private void getFileFromServerbyIncome() {
		if (choiceCompanyAdapter != null) {
			typeCompare = 1;
			ObjID = new StringBuilder();
			ObjID.append("[");
			for (int i = 0; i < mArrCompany.size(); i++) {
				if (mArrCompany.get(i).isSelected()) {
					ObjID.append("\"");
					ObjID.append(mArrCompany.get(i).getCompannyId());
					ObjID.append("\",");
				}
			}
			String mObjID = ObjID.toString().substring(0, ObjID.length() - 1)
					+ "]";
			BMSCore.getInstance(ReportDetailActivity.this)
			.taskGetDetailReportFile(
					typeCompare,
					mObjID,
					null,
					DateFrom,
					DateTo,
					mPreferences
					.getUserAuthKey(ReportDetailActivity.this),
					mListener);
		}
	}

	private void getFileFromServerbyAll() {
		if (choiceCompanyAdapter != null) {
			typeCompare = 2;
			ObjID = new StringBuilder();
			ObjID.append("[");
			for (int i = 0; i < mArrCompany.size(); i++) {
				if (mArrCompany.get(i).isSelected()) {
					ObjID.append("\"");
					ObjID.append(mArrCompany.get(i).getCompannyId());
					ObjID.append("\",");
				}
			}
			String mObjID = ObjID.toString().substring(0, ObjID.length() - 1)
					+ "]";
			BMSCore.getInstance(ReportDetailActivity.this)
			.taskGetDetailReportFile(
					typeCompare,
					mObjID,
					null,
					DateFrom,
					DateTo,
					mPreferences
					.getUserAuthKey(ReportDetailActivity.this),
					mListener);
		}
	}

	private void getFileFromServerbyOperation() {
		if (choiceCompanyAdapter != null && choiceMedicalRecordAdapter != null) {
			typeCompare = 3;
			ObjID = new StringBuilder();
			ObjID.append("[");
			for (int i = 0; i < mArrCompany.size(); i++) {
				if (mArrCompany.get(i).isSelected()) {
					ObjID.append("\"");
					ObjID.append(mArrCompany.get(i).getCompannyId());
					ObjID.append("\",");
				}
			}
			String mObjID = ObjID.toString().substring(0, ObjID.length() - 1)
					+ "]";

			StringBuilder CaseId = new StringBuilder();
			CaseId.append("[");
			for (int i = 0; i < mArrMedicalRecord.size(); i++) {
				if (mArrMedicalRecord.get(i).isSelected()) {
					CaseId.append("\"");
					CaseId.append(mArrMedicalRecord.get(i).getMedicalRecordId());
					CaseId.append("\",");
				}
			}
			String mCaseID = CaseId.toString()
					.substring(0, CaseId.length() - 1) + "]";

			BMSCore.getInstance(ReportDetailActivity.this)
			.taskGetDetailReportFile(
					typeCompare,
					mObjID,
					mCaseID,
					DateFrom,
					DateTo,
					mPreferences
					.getUserAuthKey(ReportDetailActivity.this),
					mListener);
		}
		if(btnDetail_company.getText().equals(this.getResources().getString(
				R.string.detail_company_des)) && btnDetail_medical.getText().equals(this.getResources().getString(
						R.string.detail_medical_record_des)) ){
			Toast.makeText(this, "Please select record and company!", Toast.LENGTH_SHORT).show();
		}else if(btnDetail_company.getText().equals(this.getResources().getString(
				R.string.detail_company_des)) && !btnDetail_medical.getText().equals(this.getResources().getString(
						R.string.detail_medical_record_des)) ){
			Toast.makeText(this, "Please select at least one company!", Toast.LENGTH_SHORT).show();
		}else if(!btnDetail_company.getText().equals(this.getResources().getString(
				R.string.detail_company_des)) && btnDetail_medical.getText().equals(this.getResources().getString(
						R.string.detail_medical_record_des)) ){
			Toast.makeText(this, "Please select at least one record!", Toast.LENGTH_SHORT).show();
		}
	}

	public void openFilePDF(File file) {
		try {

			// setDataAndType not set one by one
			Uri targetUri = Uri.fromFile(file);
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(targetUri, "application/pdf");
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(ReportDetailActivity.this,
					"No Application Available to View PDF", Toast.LENGTH_SHORT)
					.show();
		}
	}

	class ReportDetailListener extends BMSListener {

		@Override
		public void finishGetListMedicalRecord(final int returnCode,
				String resultMsg,
				final ArrayList<MedicalRecordModel> listRecords) {
			// TODO Auto-generated method stub
			super.finishGetListMedicalRecord(returnCode, resultMsg, listRecords);
			runOnUiThread(new Runnable() {
				public void run() {
					pBarReport.setVisibility(View.GONE);
					if (returnCode == IBMSProtocol.STATUS_CODE_OK) {
						listMedicalRescords = listRecords;
					}
				}
			});
		}

		@Override
		public void finishGetHopital(final int returnCode, String resultMsg,
				final ArrayList<Hospital> listHopital) {
			// TODO Auto-generated method stub
			super.finishGetHopital(returnCode, resultMsg, listHopital);

			runOnUiThread(new Runnable() {

				public void run() {
					pBarReport.setVisibility(View.GONE);
					if (returnCode == IBMSProtocol.STATUS_CODE_OK) {
						listHopitals.addAll(listHopital);
					}else if (returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}

		@Override
		public void finishGetListCompany(final int returnCode,
				String resultMsg, final ArrayList<CompanyModel> listComs) {
			// TODO Auto-generated method stub
			super.finishGetListCompany(returnCode, resultMsg, listComs);
			pBarReport.setVisibility(View.VISIBLE);
			runOnUiThread(new Runnable() {

				public void run() {
					pBarReport.setVisibility(View.GONE);
					if (returnCode == IBMSProtocol.STATUS_CODE_OK) {

						listCompanies.addAll(listComs);
					}else if (returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}
					
				}
			});

		}

		@Override
		public void finishGetTypeUsers(final int returnCode, String resultMsg,
				int recordNumber, final int countPage, final int groupID,
				final ArrayList<BMSUserModel> listUserModel) {
			// TODO Auto-generated method stub
			super.finishGetTypeUsers(returnCode, resultMsg, recordNumber,
					countPage, groupID, listUserModel);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					pBarReport.setVisibility(View.GONE);
					if (returnCode == IBMSProtocol.STATUS_CODE_OK) {
						if (pageNumberDoctor < countPage){
							pageNumberDoctor +=1;
						}else{
							pageNumberDoctor = -1;
						}
						listDoctors.addAll(listUserModel);
						
						
					}else if (returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}

				}
			});
		}

		@Override
		public void finishGetDetailReportbyType(final int returnCode,
				final int type,
				final ArrayList<DetailReportHospitalModel> listHospital,
				final ArrayList<DetailReportDoctorModel> listDoctor) {
			// TODO Auto-generated method stub
			super.finishGetDetailReportbyType(returnCode, type, listHospital,
					listDoctor);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					pBarReport.setVisibility(View.GONE);
					// TODO Auto-generated method stub
					if (returnCode == IBMSProtocol.STATUS_CODE_OK) {
						if (type == 1 && listHospital != null) {
							mArrHos.clear();
							mArrHos.addAll(listHospital);
							hospitalAdapter = new ReportDetailHospitalAdapter(
									ReportDetailActivity.this, mArrHos);
							lvSub.setAdapter(hospitalAdapter);
							llDetailReport.setVisibility(View.VISIBLE);
							hospitalAdapter.notifyDataSetChanged();
							Utils.setListViewHeightBasedOnChildrenforList(
									ReportDetailActivity.this, lvSub);
							Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.sussess),
									Toast.LENGTH_SHORT).show();

						} else if (type == 2 && listDoctor != null) {
							mArrReportDoctor.clear();
							mArrReportDoctor.addAll(listDoctor);
							String name= mArrReportDoctor.get(0).getDoctorName();
							detailDoctorAdapter = new ReportDetailDoctorAdapter(
									ReportDetailActivity.this, mArrReportDoctor);
							lvSub.setAdapter(detailDoctorAdapter);
							detailDoctorAdapter.notifyDataSetChanged();
							Utils.setListViewHeightBasedOnChildrenforList(
									ReportDetailActivity.this, lvSub);
							llDetailReport.setVisibility(View.VISIBLE);
							Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.sussess),
									Toast.LENGTH_SHORT).show();
						}
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}

				}
			});
		}


		@Override
		public void finishGetDetailReportFile(final int returnCode,
				final String msg, final int type, final String Link) {
			// TODO Auto-generated method stub
			super.finishGetDetailReportFile(returnCode, msg, type, Link);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					pBarReport.setVisibility(View.GONE);
					// TODO Auto-generated method stub
//					if (msg != null) {
//						Toast.makeText(ReportDetailActivity.this, msg,
//								Toast.LENGTH_SHORT).show();
//					}
					if (returnCode == IBMSProtocol.STATUS_CODE_OK
							&& Link != null) {
						new DownloadFileAsyn().execute(Link);
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}

				}
			});
		}

	}

	class DownloadFileAsyn extends AsyncTask<String, Integer, String> {

		private int count;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			pBarReport.setVisibility(View.VISIBLE);
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				URL url = new URL(params[0]);
				URLConnection conection = url.openConnection();
				conection.connect();
				int lenghtOfFile = conection.getContentLength();
				InputStream input = new BufferedInputStream(url.openStream());

				String reverseString = new StringBuffer(params[0]).reverse()
						.toString();
				int separatorIndex = reverseString.indexOf("/");
				int start = params[0].length() - separatorIndex;
				String fileName = params[0]
						.substring(start, params[0].length());

				File file = new File(PathFile, fileName);
				if (file.exists()) {
					file.delete();
				}

				OutputStream output = new FileOutputStream(file);
				byte data[] = new byte[1024];
				long total = 0;
				while ((count = input.read(data)) != -1) {
					total += count;
					publishProgress((int) ((total * 100) / lenghtOfFile));
					output.write(data, 0, count);
				}
				output.flush();
				output.close();
				input.close();

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			pBarReport.setVisibility(View.GONE);
			loadListFileFromSDCard(PathFile);
			//			fileSDCardAdapter.
			fileSDCardAdapter.notifyDataSetChanged();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			pBarReport.setProgress(values[0]);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if (fileSDCardAdapter != null && listRevertedFiles.get(position) != null) {
			openFilePDF(listRevertedFiles.get(position));
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		positionToDelete=position;
		dialog= new DeleteDialog(this);
		dialog.show();
		return false;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		IntentFilter filter = new IntentFilter();
		filter.addAction(IBMSConfig.KEY_ACTION_DELETE_FILE);
		this.registerReceiver(receiver, filter);
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		this.unregisterReceiver(receiver);
		super.onPause();
	}
	private BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			// load list medical record follow hospital
			Bundle bundle = intent.getBundleExtra("BUNDLE");
			Boolean bool = bundle.getBoolean("BOOLEAN");
			dialog.dismiss();
			if(bool==true){
				if (fileSDCardAdapter != null && listRevertedFiles.get(positionToDelete) != null) {
					Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.deleting), Toast.LENGTH_SHORT).show();
					File file = new File(PathFile, listRevertedFiles.get(positionToDelete).getName());
					if (file.exists()) {
						file.delete();
					}

					fileSDCardAdapter.notifyDataSetChanged();
					loadListFileFromSDCard(PathFile);
					Toast.makeText(ReportDetailActivity.this, ReportDetailActivity.this.getResources().getString(R.string.done), Toast.LENGTH_SHORT).show();

				}
			}

		}
	};

	

}
